const logger = require('../utils/logger');

/**
 * HTTP 请求日志中间件
 */
const requestLogger = (req, res, next) => {
  const startTime = Date.now();

  // 记录请求开始
  logger.debug(`收到请求: ${req.method} ${req.originalUrl}`, {
    method: req.method,
    url: req.originalUrl,
    ip: req.ip || req.connection.remoteAddress,
    userAgent: req.get('user-agent')
  });

  // 监听响应完成
  res.on('finish', () => {
    const responseTime = Date.now() - startTime;
    logger.logRequest(req, res, responseTime);
  });

  next();
};

module.exports = requestLogger;

